<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!--头部的布局end-->
        <!--一主体内容的布局-->
        <!-- :data指定要渲染的数据-->
        <!-- :props:指定数据中的对象的某个属性在树形组件显示及子级节点的属性名-->
        <!--@node-click:点击事件，点击某一个节点,获取节点对象数据-->
        <el-tree :data="list">
          <!-- 使用插件在el-tree内容自定义内容 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog title="新增子部门" :visible.sync="showDialog" :close-on-click-modal="false" :close-on-press-escape="false" @close="hClose">
        <!-- 新增子部门或编辑部门的子组件 -->
        <addOrEdit :id="curId" ref="myDept" :is-edit="isEdit" :origin-list="originList" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranLisToTreeList } from '@/utils'
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      showDialog: false, // 控制弹窗的显示与隐藏
      list: [

      ],
      curId: '', // 父级id
      isEdit: false, // false 新增  ||  true 编辑
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      console.log('弹层关闭喽')
      // 清空表单 ---> 在子组件
      this.$refs.myDept.resetForm()
    },
    hSuccess() {
      // 关闭弹窗,重新刷新页面
      this.showDialog = false
      this.loadDepartments()
    },
    async doDelete(id) {
      const data = await delDepartment(id)
      console.log(data)
    },
    hDel(id) {
      this.$confirm('确定要删除这条数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.doDelete(id)
        this.loadDepartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true // 将编辑状态改为true,确定当前是一个编辑操作
      this.showDialog = true
      // 解决数据不更新的方式2(1是v-if="showDialog"),在父组件中调用子组件的方法 要在子组件标签中添加ref="myDept"
      // this.$nextTick(() => {
      //   this.$refs.myDept.loadDepartmentsDetail()
      // })
    },
    hAdd(id) {
      // 显示弹层
      this.showDialog = true
      // 更新当前操作的id
      this.curId = id
      this.isEdit = false
    },
    // 发送请求,获取数据
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res.data.depts, 'res.data.depts')
        // this.list = tranLisToTreeList(res.data.depts)

        this.originList = res.data.depts.map(item => {
          const {
            name, code, id, pid } = item
          return {
            name, code, id, pid }
        })
        console.log(this.originList, 'originList')
        console.log(res, 'getDepartments')
        res.data.depts.shift() // 删除数组中第一条数据-一公司的名称
        // res.data.depts平铺数据 ---> 转化为树形数据 ---> tranLisToTreeList(res.data.depts)
        this.list = tranLisToTreeList(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

